﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PSI.Models;
using System.Text;
using System.Collections.Specialized;
using System.Web.Mvc;


namespace PSI.Services
{
    public class KnowledgeService:PSIBaseService
    {
        //新增或修改文章类别
        public int AddOREditEssayType(KnowledgeType model)
        {
            var str = new StringBuilder();
            var p = CreateDbParameters();
            var result = 0;
            if (model.id==Guid.Empty)
            {
                str.Append("insert KnowledgeType values (@id,@name)");
                p.AddWithValue("id", Guid.NewGuid());
                p.AddWithValue("name", model.name);
                result= Execute(str.ToString(), p);
            }
            else
            {
                str.Append("update KnowledgeType set name=@name where id=@id");
                p.AddWithValue("id", model.id);
                p.AddWithValue("name", model.name);
                result = Execute(str.ToString(),p);
            }
            return result;
        }

        //返回所有的文章类别列表
        public List<KnowledgeType> AllKtypeitems()
        {
            var result = new List<KnowledgeType>();
            var str = new StringBuilder("select * from KnowledgeType");
            Query(str.ToString(), ready =>
            {
                result.Add(new KnowledgeType()
                {
                    id = ready.GetGuid(ready.GetOrdinal("id")),
                    name = ready.GetString(ready.GetOrdinal("name")),
                });

            });
            return result;
        }

        //删除一条文章类别记录
        public int DelEssaySing(string id)
        {
            var str = new StringBuilder("delete KnowledgeType where id=@id");
            var p = CreateDbParameters();
            p.AddWithValue("id", id);
            return Execute(str.ToString(), p);        
        }


        /// <summary>
        /// 发布文章
        /// </summary>
        /// <param name="formData"></param>
        /// <returns></returns>
       
        public int ReleaseEassy(NameValueCollection formData)
        {
            var id = formData["id"];
            var type = formData["etype"];
            var title=formData["title"];
            var file1 = formData["readyone"];
            var file2 = formData["readytwo"];
            var content = formData["bio"];

            var p = CreateDbParameters();

            if (string.IsNullOrEmpty(id.ToString()))
            {
                var str = new StringBuilder("insert KnowledgeTheme values (@id,@KtypeID,@Title,@Contents,@File_one,@File_two,@CreatTime,@CreatPrent)");

                p.AddWithValue("id", Guid.NewGuid());
                p.AddWithValue("KtypeID", type);
                p.AddWithValue("Title", title);
                p.AddWithValue("Contents", content);
                p.AddWithValue("File_one", file1);
                p.AddWithValue("File_two", file2);
                p.AddWithValue("CreatTime", DateTime.Now);
                p.AddWithValue("CreatPrent", Services.UserService.GetLoginUserName());
                return Execute(str.ToString(), p);
            }
            else
            {
                var str = new StringBuilder("update KnowledgeTheme set KtypeID=@KtypeID,Title=@Title,Contents=@Contents,File_one=@File_one,File_two=@File_two where id=@id");
                p.AddWithValue("id", id);
                p.AddWithValue("KtypeID", type);
                p.AddWithValue("Title", title);
                p.AddWithValue("Contents", content);
                p.AddWithValue("File_one", file1);
                p.AddWithValue("File_two", file2);
                return Execute(str.ToString(), p);
            }
           
        }

        //返回所有的文章列表
        public List<KnowledgeTheme> GetAllAcr(string pareid)
        {
            var result = new List<KnowledgeTheme>();
            var str = new StringBuilder("select * from KnowledgeTheme where 1=1 ");
            var p = CreateDbParameters();
            if (!string.IsNullOrEmpty(pareid))
            {
                str.Append(" and KtypeID=@kid");
                p.AddWithValue("kid",pareid);
            }
            Query(str.ToString(),p, ready =>
            {
                result.Add(new KnowledgeTheme{
                    id=ready.GetGuid(ready.GetOrdinal("id")),
                    KTypeID = ready.GetGuid(ready.GetOrdinal("KtypeID")),
                    title = ready.GetString(ready.GetOrdinal("Title")),
                    Contents = ready.GetString(ready.GetOrdinal("Contents")),
                    FileOne = ready.GetString(ready.GetOrdinal("File_one")),
                    FileTwo = ready.GetString(ready.GetOrdinal("File_two")),
                    CreatTime = ready.GetDateTime(ready.GetOrdinal("CreatTime")),
                    CreatPerson = ready.GetString(ready.GetOrdinal("CreatPrent")),
                            
                });

            });
            return result;
            
        }

        //得到指定的文章
        public KnowledgeTheme GetSingAcr(string id)
        {
            var result = new KnowledgeTheme();
            var str = new StringBuilder("select * from KnowledgeTheme where id=@id ");
            var p = CreateDbParameters();
            p.AddWithValue("id", id);
            Query(str.ToString(), p, ready =>
            {
              result= new KnowledgeTheme
                {
                    id = ready.GetGuid(ready.GetOrdinal("id")),
                    KTypeID = ready.GetGuid(ready.GetOrdinal("KtypeID")),
                    title = ready.GetString(ready.GetOrdinal("Title")),
                    Contents = ready.GetString(ready.GetOrdinal("Contents")),
                    FileOne = ready.GetString(ready.GetOrdinal("File_one")),
                    FileTwo = ready.GetString(ready.GetOrdinal("File_two")),
                    CreatTime = ready.GetDateTime(ready.GetOrdinal("CreatTime")),
                    CreatPerson = ready.GetString(ready.GetOrdinal("CreatPrent")),

                };

            });
            return result;
        }

        public int DeleteAcrSing(string id)
        {
            var str = new StringBuilder("delete KnowledgeTheme where id=@id");
            var p = CreateDbParameters();
            p.AddWithValue("id", id);
            return Execute(str.ToString(),p);
        }

        public int AddReply(NameValueCollection formData)
        {
            var KthemeID = formData["KthemeID"];
            var contens = formData["contens"];

            var str = new StringBuilder("insert KnowledgeUserReply values (@id,@KthemeID,@Contents,@CreatTime,@CreatPrent)");
            var p = CreateDbParameters();
            p.AddWithValue("id",Guid.NewGuid());
             p.AddWithValue("KthemeID",KthemeID);
             p.AddWithValue("Contents",contens);
             p.AddWithValue("CreatTime",DateTime.Now);
             p.AddWithValue("CreatPrent",UserService.GetLoginUserName());
             return Execute(str.ToString(), p);
        }

        public List<KnowledgeUserReply> GetAllReply(string themeid)
        {
            var result = new List<KnowledgeUserReply>();
            var str = new StringBuilder("select * from KnowledgeUserReply where KthemeID=@id order by CreatTime DESC");
            var p = CreateDbParameters();
            p.AddWithValue("id",themeid);
            Query(str.ToString(), p, ready =>
            {
                result.Add(new KnowledgeUserReply {
                    id = ready.GetGuid(ready.GetOrdinal("id")),
                    KthemeID = ready.GetGuid(ready.GetOrdinal("KthemeID")),
                    Contents = ready.GetString(ready.GetOrdinal("Contents")),
                    CreatTime = ready.GetDateTime(ready.GetOrdinal("CreatTime")),
                    CreatPerson = ready.GetString(ready.GetOrdinal("CreatPrent")),
                    
                });


            });
            return result;
        }

        /// <summary>
        /// 首页知识管理列表
        /// </summary>
        /// <returns></returns>
        public List<KnowledgeTheme> MainPageInfo()
        {
            var resulr = new List<KnowledgeTheme>();
            var str = new StringBuilder("select [id],(select name from [psi2014].[dbo].[KnowledgeType] where a.KtypeID=id ),Title from [psi2014].[dbo].[KnowledgeTheme] as a order by CreatTime desc");

            Query(str.ToString(), ready =>
            {
                resulr.Add(new KnowledgeTheme() { 
                 id=ready.GetGuid(0),
                 Contents=ready.GetString(1),
                 title=ready.GetString(2)
                });

            });

            return resulr;
        }


    }
}